﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.ExtensibleStorage;
using Autodesk.Revit.UI;
using YJKSparseDistance.ModelLayer;
using YJKSparseDistance.Properties;
using YJKRevitKernel.FormUtility.ModelessFormControlSuite.ModelLayer;
using YJKInspectModelFunc;

namespace YJKSparseDistance.ViewLayer
{
    public partial class ResultForm : System.Windows.Forms.Form, IModelessFormNoneSwitch
	{
		public string UseGuid
		{
			get
			{
				return this.m_UseGuid;
			}
			set
			{
				this.m_UseGuid = value;
			}
		}

		public UIDocument UseUIDocument
		{
			get
			{
				return this.m_useUIDocument;
			}
			private set
			{
				this.m_useUIDocument = value;
			}
		}

		public LocationAWayBean TempLocationAWayBean
		{
			get
			{
				return this.m_tempLocationAWayBean;
			}
			private set
			{
				this.m_tempLocationAWayBean = value;
			}
		}

		public List<int> LstElementIdsNeedDelete
		{
			get
			{
				return this.m_lstElementIdsNeedDelete;
			}
		}

		public Dictionary<Area, List<ResultBean>> LstNeedTrust
		{
			get
			{
				return this.m_lstNeedTrust;
			}
			private set
			{
				this.m_lstNeedTrust = value;
			}
		}

		public ResultForm(List<List<AreaCheckResultPacker>> input, string useName, IWin32Window revitHandle, UIDocument inputDoc, string inputGuid = null)
		{
			this.InitializeComponent();
			this.dataGridView_Result.AutoGenerateColumns = false;
			this.m_lstlstAllResult = input;
			this.Text = useName;
			this.m_RevitHandle = revitHandle;
			this.UseUIDocument = inputDoc;
			if (inputGuid != null)
			{
				this.m_UseGuid = inputGuid;
			}
			this.MakeDataTable();
		}

		private void ResultForm_Load(object sender, EventArgs e)
		{
		}

		private void MakeDataTable()
		{
			this.dataGridView_Result.DataSource = null;
			this.m_useDataTable = new DataTable();
			this.m_useDataTable.Columns.Add("LevelName", typeof(string));
			this.m_useDataTable.Columns.Add("FireaAreaName", typeof(string));
			this.m_useDataTable.Columns.Add("RoomName", typeof(string));
			this.m_useDataTable.Columns.Add("RuleValue", typeof(string));
			this.m_useDataTable.Columns.Add("DesignValue", typeof(string));
			this.m_useDataTable.Columns.Add("HashValue", typeof(int));
			foreach (List<AreaCheckResultPacker> list in this.m_lstlstAllResult)
			{
				int num = 0;
				foreach (AreaCheckResultPacker areaCheckResultPacker in list)
				{
					for (int i = 0; i < areaCheckResultPacker.LstErrorResult.Count; i++)
					{
						ResultBean resultBean = areaCheckResultPacker.LstErrorResult[i];
						List<string> allValues;
						Entity entity;
						if (!InsetDataUtility.TryGetAddedValue(this.UseGuid, areaCheckResultPacker.ThisArea, out allValues, out entity) || !Utility.IfResultTrusted(resultBean, allValues))
						{
							if (num == 0)
							{
								this.m_useDataTable.Rows.Add(new object[]
								{
									areaCheckResultPacker.ThisLevel.Name,
									YJKInspectModelFunc.Utility.GetFireAreaString(areaCheckResultPacker.ThisArea),
									this.GetRoomUseName(resultBean.StartPoint.ThisRoom),
									string.Format("{0:F}", this.FeetToM(resultBean.RuleValue)) + "m",
									string.Format("{0:F}", this.FeetToM(resultBean.Distance)) + "m",
									resultBean.GetHashCode()
								});
							}
							else
							{
								this.m_useDataTable.Rows.Add(new object[]
								{
									string.Empty,
									YJKInspectModelFunc.Utility.GetFireAreaString(areaCheckResultPacker.ThisArea),
									this.GetRoomUseName(resultBean.StartPoint.ThisRoom),
									string.Format("{0:F}", this.FeetToM(resultBean.RuleValue)) + "m",
									string.Format("{0:F}", this.FeetToM(resultBean.Distance)) + "m",
									resultBean.GetHashCode()
								});
							}
							num++;
						}
					}
				}
			}
			this.dataGridView_Result.DataSource = this.m_useDataTable;
			this.dataGridView_Result.Columns[4].DefaultCellStyle.ForeColor = System.Drawing.Color.Red;
		}

		private string GetRoomUseName(Room input)
		{
			string result;
			try
			{
				if (this.m_dicRoomTagAndNamePairs == null && input.Document != null)
				{
					this.PrepareRoomNameCache(input.Document);
				}
				string text;
				if (this.m_dicRoomTagAndNamePairs == null || !this.m_dicRoomTagAndNamePairs.ContainsKey(input.Id.IntegerValue))
				{
					text = string.Empty;
				}
				else
				{
					text = this.m_dicRoomTagAndNamePairs[input.Id.IntegerValue];
				}
				result = text;
			}
			catch (Exception)
			{
				result = string.Empty;
			}
			return result;
		}

		private void PrepareRoomNameCache(Autodesk.Revit.DB.Document inputDoc)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(inputDoc);
			filteredElementCollector.OfCategory((BuiltInCategory)Enum.Parse(typeof(BuiltInCategory), Resources.Str_CategoryOfRoomTag));
			IEnumerable<Element> enumerable = filteredElementCollector.WhereElementIsNotElementType().ToElements();
			this.m_dicRoomTagAndNamePairs = new Dictionary<int, string>();
			foreach (Element element in enumerable)
			{
				if (element != null && element is RoomTag)
				{
					RoomTag roomTag = element as RoomTag;
					if (roomTag.Room != null)
					{
						if (!this.m_dicRoomTagAndNamePairs.ContainsKey(roomTag.Room.Id.IntegerValue))
						{
							this.m_dicRoomTagAndNamePairs.Add(roomTag.Room.Id.IntegerValue, this.GetARoomName(roomTag.Room));
						}
						else
						{
							this.m_dicRoomTagAndNamePairs[roomTag.Room.Id.IntegerValue] = this.GetARoomName(roomTag.Room);
						}
					}
				}
			}
		}

		private string GetARoomName(Room input)
		{
			string result;
			try
			{
				Parameter parameter = input.get_Parameter(BuiltInParameter.ROOM_NAME);
				if (parameter == null)
				{
					result = string.Empty;
				}
				else
				{
					result = parameter.AsString();
				}
			}
			catch (Exception)
			{
				result = string.Empty;
			}
			return result;
		}

		private double FeetToM(double input)
		{
			return AssistFunc.feetToMM(input) / 1000.0;
		}

		private ResultBean GetResultBeanInDataTable(int rowIndex, out Area useArea, bool ifRemove = false)
		{
			ResultBean resultBean = null;
			useArea = null;
			int num = (int)this.m_useDataTable.Rows[rowIndex].ItemArray[5];
			for (int i = 0; i < this.m_lstlstAllResult.Count; i++)
			{
				List<AreaCheckResultPacker> list = this.m_lstlstAllResult[i];
				for (int j = 0; j < list.Count; j++)
				{
					AreaCheckResultPacker areaCheckResultPacker = list[j];
					int k = 0;
					while (k < areaCheckResultPacker.LstErrorResult.Count)
					{
						ResultBean resultBean2 = areaCheckResultPacker.LstErrorResult[k];
						if (num == resultBean2.GetHashCode())
						{
							resultBean = resultBean2;
							if (ifRemove)
							{
								useArea = areaCheckResultPacker.ThisArea;
								areaCheckResultPacker.LstErrorResult.Remove(resultBean2);
								break;
							}
							break;
						}
						else
						{
							k++;
						}
					}
					if (resultBean != null)
					{
						break;
					}
				}
				if (resultBean != null)
				{
					break;
				}
			}
			return resultBean;
		}

		private void button_Cancle_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void btn_Trust_Click(object sender, EventArgs e)
		{
			List<int> list = new List<int>();
			int num = 0;
			Area area = null;
			IEnumerator enumerator = ((IEnumerable)this.dataGridView_Result.Rows).GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if ((bool)((DataGridViewRow)enumerator.Current).Cells[5].EditedFormattedValue)
					{
						list.Add(num);
					}
					num++;
				}
			}
			foreach (int rowIndex in list)
			{
				Area area2;
				ResultBean resultBeanInDataTable = this.GetResultBeanInDataTable(rowIndex, out area2, true);
				area = null;
				if (resultBeanInDataTable != null && area2 != null)
				{
					foreach (Area area3 in this.LstNeedTrust.Keys)
					{
						if (area3.Id.IntegerValue == area2.Id.IntegerValue)
						{
							area = area3;
						}
					}
					if (area == null)
					{
						this.LstNeedTrust.Add(area2, new List<ResultBean>());
						area = area2;
					}
					this.LstNeedTrust[area].Add(resultBeanInDataTable);
				}
			}
			this.MakeDataTable();
		}

		private void Event_GridViewDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			int rowIndex = e.RowIndex;
			this.m_bIfContinue = true;
			this.m_bIfBreak = false;
			this.m_bIfManualEsc = false;
			this.TempLocationAWayBean = null;
			if (rowIndex != -1)
			{
				Area area;
				ResultBean resultBeanInDataTable = this.GetResultBeanInDataTable(rowIndex, out area, false);
				this.TempLocationAWayBean = new LocationAWayBean
				{
					UseBean = resultBeanInDataTable,
					UseUIDoc = this.UseUIDocument,
					UseForm = this
				};
			}
			this.SendEscMessageToRevitAndClearSelect();
		}

		private void dataGridView_Result_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			this.Event_GridViewDoubleClick(sender, e);
		}

		protected override bool ProcessKeyEventArgs(ref Message m)
		{
			int num = 256;
			int num2 = 27;
			if (m.Msg == num && m.WParam.ToInt32() == num2)
			{
				base.Close();
			}
			return base.ProcessKeyEventArgs(ref m);
		}

		public bool IfContinue
		{
			get
			{
				return this.m_bIfContinue;
			}
		}

		public bool IfBreak
		{
			get
			{
				return this.m_bIfBreak;
			}
		}

		public bool IfManualEsc
		{
			get
			{
				return this.m_bIfManualEsc;
			}
		}

		public void ShowForm(IWin32Window parentForm = null)
		{
			if (this.m_RevitHandle != null)
			{
				base.Show(this.m_RevitHandle);
				return;
			}
			base.Show(this.m_RevitHandle);
		}

		public void CloseForm()
		{
			if (!base.IsDisposed)
			{
				base.Close();
			}
		}

		public void ResetContinueBreak()
		{
			this.m_bIfContinue = false;
			this.m_bIfBreak = true;
			this.m_bIfManualEsc = true;
		}

		public void SendEscMessageToRevitAndClearSelect()
		{
			RevitVersionFuncs.ClearSelection(this.UseUIDocument.Selection);
			if (this.m_RevitHandle != null)
			{
                YJKInspectModelFunc.Utility.SendEscToHandle(this.m_RevitHandle);
			}
		}

		private Dictionary<Area, List<ResultBean>> m_lstNeedTrust = new Dictionary<Area, List<ResultBean>>();

		private List<List<AreaCheckResultPacker>> m_lstlstAllResult = new List<List<AreaCheckResultPacker>>();

		private DataTable m_useDataTable;

		private List<int> m_lstElementIdsNeedDelete = new List<int>();

		private bool m_bIfContinue;

		private IWin32Window m_RevitHandle;

		private LocationAWayBean m_tempLocationAWayBean;

		private UIDocument m_useUIDocument;

		private string m_UseGuid = Resources.Str_GuidForEvacuation;

		private Dictionary<int, string> m_dicRoomTagAndNamePairs;
	}
}
